<template>
  <div class="user-info">
    <page-header :model="model"></page-header>
    <div class="info-detail">
      <div class="detail-item">
        <span>头像</span>
        <img :src="model.avatar" alt="">
      </div>
      <div class="detail-item">
        <span>昵称</span>
        <span>{{model.name}}</span>
      </div>
      <div class="detail-item">
        <span>UID</span>
        <span>{{model._id}}</span>
      </div>
      <div class="detail-item">
        <span>个性签名</span>
        <span>{{model.desc}}</span>
      </div>
    </div>
    <div class="info-exit">
      <div class="exit-btn" @click="exit">
        退出登录
      </div>
    </div>
  </div>
</template>

<script>
import { Dialog } from 'vant'
import PageHeader from '../components/common/PageHeader'
export default {
  components: { PageHeader },
  data () {
    return {
      model: {}
    }
  },
  methods: {
    async fetchUser () {
      const res = await this.$http.get('/user')
      this.model = res.data
    },
    exit () {
      Dialog.confirm({
        title: '退出登录',
        message: '确定要退出吗？请三思！！'
      })
        .then(() => {
          // on confirm
          localStorage.clear()
          this.$router.push('/login')
        })
        .catch(() => {
          // on cancel
        })
    }
  },
  created () {
    this.fetchUser()
  }
}
</script>

<style lang="scss">
.user-info {
  .info-detail {
    background-color: #fff;
    margin-top: 10px;
    .detail-item {
      box-sizing: border-box;
      padding: 8px 10px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 2px solid #f9f9f9;
      font-size: 14px;
      img {
        width: 45px;
        height: auto;
        border-radius: 50%;
      }
      span:nth-child(1) {
        color: #505050;
      }
      span:nth-child(2) {
        color: #999;
      }
    }
  }
  .info-exit {
    margin-top: 15px;
    .exit-btn {
      width: 100%;
      box-sizing: border-box;
      padding: 8px 10px;
      background-color: #fff;
      color: #505050;
      font-size: 14px;
      text-align: center;
    }
  }
}
</style>
